package com.lemon.tuvi2015;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.lemon.tuvi2015.adapter.SmsKuteAdapter;
import com.lemon.tuvi2015.adapter.VankhanAdapter;
import com.lemon.tuvi2015.adapter.ZodiacAdapter;
import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.CMD;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;

public class SmsKuteActivity extends Activity {
	CustomActionbar actionbar;
	ArrayList<GlobalObject> listZodiac;
	String ID;
	ProgressBar progressBar;
	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smskute_list);
		initView();
		initActionbar();

		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.KEY))
			ID = (String) extras.getString(TagName.KEY);
		if (ID != null && !ID.equals(""))
			new AsyncTaskParseJson().execute("");
	}

	void initView() {
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		listView = (ListView) findViewById(R.id.listView);
		listView.setEmptyView(progressBar);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				BaseEngine.startDetailsActivityObject_Key(SmsKuteActivity.this, listZodiac.get(position),CMD.SMSKUTE);
				Intent intent = new Intent(SmsKuteActivity.this, SmsListActivity.class);
				intent.putExtra(TagName.KEY, listZodiac.get(position));
				startActivity(intent);
			}
		});
	}

	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		actionbar.setActionbar(R.string.home_menu_smskute, listenerBack, null);
	}

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.getJson(CMD.HOME_ITEM_DETAILS, ID);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				BaseEngine.showLogformLongString("", result);
				try {
					if(!JsonParser.readStatus(result).equals("")){
						listZodiac = JsonParser.readlistZodiac(result);
						if (listZodiac != null && listZodiac.size() > 0) {
							listView.setAdapter(new SmsKuteAdapter(getApplicationContext(), listZodiac));
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
